<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="6" >
            <a-form-model-item label="单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
              <a-input v-model="model.code" placeholder="请输入单号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="员工编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="usercode">
              <a-input v-model="model.usercode" placeholder="请输入员工编码" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="员工名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="username">
              <a-input v-model="model.username" placeholder="请输入员工名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="微信号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="wexcode">
              <a-input v-model="model.wexcode" placeholder="请输入微信号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cdate">
              <j-date placeholder="请选择日期" v-model="model.cdate" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="厂商id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="manufacturer">
              <j-popup
                v-model="model.manufacturer"
                field="manufacturer"
                org-fields="id,name"
                dest-fields="manufacturer,manufacturername"
                code="SelectorManufacturer"
                :multi="true"
                @input="popupCallback"
                />

            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="厂商名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="manufacturername">
              <a-input v-model="model.manufacturername" placeholder="请输入厂商名称" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="款号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stylenumber">
              <j-popup
                v-model="model.stylenumber"
                field="stylenumber"
                org-fields="id,stylenumber"
                dest-fields="proid,stylenumber"
                code="erp_procedure"
                :multi="true"
                @input="popupCallback"
                />

            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="工序id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="proid">
              <a-input v-model="model.proid" placeholder="请输入工序id" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remarks">
              <a-input v-model="model.remarks" placeholder="请输入备注" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="actstatus">
              <a-input v-model="model.actstatus" placeholder="请输入状态" ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="工序完工单" :key="refKeys[0]" :forceRender="true">
       <!-- <j-vxe-table
          keep-source
          :ref="refKeys[0]"
          :loading="erpProcompletionBTable.loading"
          :columns="erpProcompletionBTable.columns"
          :dataSource="erpProcompletionBTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :toolbar="true"
          />-->
          <nbcio-editable-table
          :ref="refKeys[0]"
          v-on:beforeAdd="beforeAdd"
          :isAdd="isAdd"
          :loading="erpProcompletionBTable.loading"
          :columns="erpProcompletionBTable.columns"
          :dataSource="erpProcompletionBTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          @valueChange="onValueChange"
          :rowSelection="true"
          :actionButton="true"
          >

          </nbcio-editable-table>
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

  import { getAction,putAction } from '@/api/manage'
  //import { JVxeTableModelMixin } from '@/mixins/JVxeTableModelMixin.js'
  import { NbcioTableModelMixin } from '@/mixins/NbcioTableModelMixin'
  import { GoodsModalMixin } from '../mixins/GoodsModalMixin.js'
  import { JVXETypes } from '@/components/jeecg/JVxeTable'
  import { getRefPromise,VALIDATE_FAILED} from '@/components/jeecg/JVxeTable/utils/vxeUtils.js'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  const ruleBaseURL = '/sys/fillRule/executeRuleByCode/'

  export default {
    name: 'ErpProcompletionForm',
    mixins: [JVxeTableModelMixin],
    components: {
      JFormContainer,
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        model:{
         },
        //是否容许增加子表单行记录,false为不可以添加记录
        isAdd: false,
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
        },
        refKeys: ['erpProcompletionB', ],
        tableKeys:['erpProcompletionB', ],
        activeKey: 'erpProcompletionB',
        // 工序完工单
        erpProcompletionBTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '行号',
              key: 'row',
               type: JVXETypes.input,
               disabled:true,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '工序号',
              key: 'processnum',
              type: JVXETypes.popup,
              popupCode:"",
              field:"",
              orgFields:"",
              destFields:"",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '工序名称',
              key: 'processname',
               type: JVXETypes.input,
               disabled:true,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '款号',
              key: 'stylenumber',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '码号',
              key: 'number',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '颜色',
              key: 'colour',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '数量',
              key: 'quantity',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '单价',
              key: 'unitprice',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '金额',
              key: 'money',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '是否已结算',
              key: 'balance',
              type: JVXETypes.select,
              options:[],
              dictCode:"balance",
              disabled:true,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        url: {
          rule: {
          // 编码规则校验 填写自己的编码规则
            orderCode: ruleBaseURL + 'ces_order_rule'
          },
          add: "/procompletion/erpProcompletion/add",
          edit: "/procompletion/erpProcompletion/edit",
          queryById: "/procompletion/erpProcompletion/queryById",
          erpProcompletionB: {
            list: '/procompletion/erpProcompletion/queryErpProcompletionBByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
    },
    methods: {
      getOrderCode() {
        putAction(this.url.rule.orderCode, this.model).then(res => {
          // 执行成功，获取返回的订单编号值，并赋到页面上
          if (res.success) {
            //赋值给自己定义的订单编号
            this.model.orderCode = res.result
          }
        })
      },
      addBefore(){
      this.getOrderCode();
        this.erpProcompletionBTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.erpProcompletionB.list, params, this.erpProcompletionBTable)
        }
      },
      //校验所有一对一子表表单
        validateSubForm(allValues){
            return new Promise((resolve,reject)=>{
              Promise.all([
              ]).then(() => {
                resolve(allValues)
              }).catch(e => {
                if (e.error === VALIDATE_FAILED) {
                  // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
                  this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
                } else {
                  console.error(e)
                }
              })
            })
        },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          erpProcompletionBList: allValues.tablesValue[0].tableData,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
      //子表新增前校验
     beforeAdd() {
       // if (this.checkSc() === false) {
       //     this.$message.error('请先选择仓库！')
       // }
     },
     //检查是否有仓库信息
     checkSc() {
       this.param.sc_id = this.model.scId;
       console.log("beforeAdd this.param.sc_id=",this.param.sc_id)
       if (this.param.sc_id === '' || this.param.sc_id === undefined) {
         this.isAdd = false
         return false
       }
       else {
         this.erpSaleOutDetailTable.columns[0].param = this.param;
         this.isAdd = true;
         return true
       }
     },
     popupCallback(value,row){
      this.model = Object.assign(this.model, row);
     },

    }
  }
</script>

<style scoped>
</style>
